<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="dialogFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="手册" value="1"></el-option>
              <!-- <el-option label="版本" value="2"></el-option> -->
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="name" label="操作手册"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="author" label="作者"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="download_count" label="下载次数"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="140" align="center" prop="create_time" label="上传时间">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getdownloadManual(scope.row)">下载</el-button>
            <el-button size="mini" @click="getdeleteManual(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination background :current-page.sync="listQuery.startSize" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加 -->
    <el-dialog title="操作系统上传" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" :rules="rules" ref="ruleForm">
        <el-form-item label="手册名字" label-width="100px" prop="name">
          <el-input v-model="addForm.name" class="add_select" placeholder="请输入手册名字" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="手册说明" label-width="100px" prop="introduce">
          <el-input v-model="addForm.introduce" class="add_select" placeholder="请输入手册说明"></el-input>
        </el-form-item>
        <el-form-item label="作者" label-width="100px" prop="author">
          <el-input v-model="addForm.author" class="add_select" placeholder="请输入作者" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="版本" label-width="100px" prop="version">
          <el-input v-model.number="addForm.version" class="add_select" placeholder="请输入版本" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="手册上传" label-width="100px" class="snElsx" prop="address">
          <div class="addPut">
            <el-input autocomplete="off" placeholder="请点击上传，上传pdf文件" v-model="addForm.address"></el-input>
            <el-upload class="upload-demo dlePut" style="top:0;right:10px" action accept=".pdf" :auto-upload="false" :on-change="compileapkAvatarUpload" :show-file-list="false">
              <el-button round size="mini">上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <Debounce :time='500' :isDebounce="3">
          <el-button type="primary" @click.native="getaddManual('ruleForm')" size="mini" round>确 定</el-button>
        </Debounce>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryManual, downloadManual, importManual, addManual, deleteManual } from "@/api/handbook.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["操作手册", "作者", "下载次数", "上传时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "1", //搜索条件
      //添加按钮控制
      dialogFormVisible: false,
      //添加表单
      addForm: {
        name: "",
        introduce: "",
        author: "",
        version: "",
        address: "",
        userId: localStorage.getItem("ID"),
        lastIp: localStorage.getItem("Ip")
      },
      rules: {
        name: [{ required: true, message: "请输入手册名字", trigger: "blur" }],
        introduce: [{ required: true, message: "请输入手册说明", trigger: "blur" }],
        author: [{ required: true, message: "请输入作者", trigger: "blur" }],
        version: [{ required: true, type: 'number', min: 0, message: "请输入版本号,只能是数字类型", trigger: "blur" }],
        address: [{ required: true, message: "请上传手册", trigger: "blur" }]
      },
      //删除弹窗
      deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
      listQuery: { startSize: 1, pageSize: 10, name: undefined, version: undefined },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryManual()
  },
  methods: {
    //删除
    getdeleteManual(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), id: this.deleteDialogs.deleteId }
      deleteManual(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryManual()
      })
    },
    //添加手册信息
    getaddManual(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addManual(addForm).then(res => {
            this.dialogFormVisible = false
            this.$message.success(res.msg)
            this.getqueryManual()
          }).catch(err => {
            this.dialogFormVisible = false
            this.getqueryManual()
          })
        } else {
          return false
        }
      })
    },
    //手册上传pdf格式
    compileapkAvatarUpload(file) {
      var fd = new FormData()
      fd.append('file', file.raw)
      importManual(fd).then(res => {
        this.addForm.address = res.data.filePath
      })
    },
    //操作手册下载
    getdownloadManual(data) {
      downloadManual({ id: data.id }).then(res => {
        window.open(res.data.address)
      })
    },
    getqueryManual(startSize = 1, pageSize = 10, type = 1, parameter) {
      queryManual(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
        console.log('请求失败', res.data)
      }).catch(err => {
        
        this.browserTable = []
      })
    },
    //搜索,1:手册名，2:版本查询
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.name = this.searchTable
          break;
        case '2':
          this.listQuery.version = this.searchTable == '白名单' ? 1 : 0
          break;
      }
      this.getqueryManual()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryManual();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryManual();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, name: undefined, version: undefined }
      this.getqueryManual()
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = {
          name: "",
          introduce: "",
          author: "",
          version: "",
          address: ""
        }
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>